layui.use('table', function () {
  var table = layui.table;

  //方法级渲染
  table.render({
    elem: '#LAY_table_user'
    , url: 'all_clock'
    , cols: [[
      { field: 'id', title: 'ID', width: 80, sort: true, fixed: true }
      , { field: 'name', title: '姓名', width: 80, sort: true }
      , { field: 'sex', title: '性别', width: 80, sort: true }
      , { field: 'dept', title: '部门', width: 80, sort: true }
      , { field: 'check_time', title: '签到时间', width: 120, sort: true, edit: true }
      , { field: 'check_state', title: '签到状态', width: 120, sort: true, edit: true }
    ]]
    , id: 'testReload'
    , page: true
    , height: 600
  });
  var $ = layui.$, active = {
    reload: function () {
      var demoReload = $('#demoReload');

      table.reload('testReload', {
        page: {
          curr: 1
        },
        where: {
          keyword: demoReload.val()
        }
      });
    }
  };
  $('.demoTable .layui-btn').on('click', function () {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  table.on('tool(useruv)', function (obj) {
    var data = obj.data;
    if (obj.event === 'detail') {
      layui.use('layer', function () {
        layer.open({
          type: 2,
          content: ["detail?id=" + data.id],   //添加页，后台controller转发到指定页
          area: ["480px", "480px"],
          title: ['用户查看'],
          fixed: false,
          //maxmin: true,
          shadeClose: true,
          end: function () {  //刷新页面
            location.reload();
          }
        })
      })
    } else if (obj.event === 'del') {
      layer.confirm('真的删除行么', function (index) {
        console.log(data);
        $.ajax({
          url: "del",
          type: "POST",
          data: { "id": data.id },
          dataType: "json",
          success: function (data) {

            if (data.state == 1) {
              //删除这一行
              obj.del();
              //关闭弹框
              layer.close(index);
              layer.msg("删除成功", { icon: 6 });
            } else {
              layer.msg("删除失败", { icon: 5 });
            }
          }
        });
      });
    } else if (obj.event === 'edit') {
      layui.use('layer', function () {
        layer.open({
          type: 2,
          content: ["edit?id=" + data.id],   //添加页，后台controller转发到指定页
          area: ["600px", "400px"],
          title: ['用户修改'],
          fixed: false,
          //maxmin: true,
          shadeClose: true,
          end: function () {  //刷新页面
            location.reload();
          }
        })
      })
    }
  });
});



function add() {
  layui.use('layer', function () {
    layer.open({
      type: 2,
      content: ["preAdd"],   //添加页，后台controller转发到指定页
      area: ["600px", "400px"],
      title: ['用户添加'],
      fixed: false,
      //maxmin: true,
      shadeClose: true,
      end: function () {  //刷新页面
        location.reload();
      }
    })
  })
}